const mixin = {
	data(){
			return{
		search_visible: false,
		search_params:{},
		init_data:{},
		init_show: false,
		primary_key: 'id',
		table_data:{
			loading: true,
			list: [],
			checked_list: [],
			checked_ids: [],
			init_ids: [],
			radio_id: null,
		},
		page:{
			current: 1,
			size: 30,
			total: 0,
		},
		page_sizes:[15,20,30,60,100],
		dlg_addEdit:{
			visible: false,
			type: null,
			row: {}
		},
		columns:[],
			}
},
// watch:{
// 	// 监听列表的变化，初始化选中的值
// 	'table_data.init_ids'(newVal){
// 		let data = this.table_data;
// 		this.$nextTick( ()=> {
// 			data.list.forEach((item,i) => {
// 				if(newVal.indexOf(item.id) != -1){
// 					this.$refs.multipleTable.toggleRowSelection(data.list[i],true);
// 				}
// 			})
// 		})
// 	},
// },
provide () {
	return {
		initData: this.init_data
	}
},
	methods:{
	getList(page, size) {
		const params = Object.assign({}, this.search_params);
		params.current = page || 1;
		params.size = size || this.page.size;
		
		this.table_data.loading = true;
		this.$request('get', this.list_api, params, (data, msg) => {
			this.$util.resetArray(this.table_data.list, data.records);
			this.page.total = data.total;
			this.table_data.loading = false;
			this.getListCallback();
		}, (msg) => {
			this.table_data.loading = false;
		})
	},
	getListCallback(){

	},
	pageSizeChange(val){
		this.page.size=val;
		this.getList(1, val);
	},	
	selectCheckbox(val){
		this.table_data.checked_list = val;
		this.table_data.checked_ids = val.map((item) => {
			return item[this.primary_key];
		});
	},
	selectRadio(row){
		//赋值给radio
		this.table_data.radio_id = row[this.primary_key];
	},
	handleAddEdit(type, data){
		this.dlg_addEdit.visible = true;
		this.dlg_addEdit.type = type;
		this.dlg_addEdit.row = data;
		this.$refs.addEdit.init(type,data);
	},
	setName(id, type){
		let type_name = type.indexOf('_list') == -1 ? type+'_list' : type;
		let list = this.init_data[type_name];
		if(id != null){
			let name;
			let arr = list.filter(item => item.id == id);
			if(arr.length == 1){
				name = arr[0].name;
			}else{
				name = '';
			}				
						return name;
		}else{
			return '';
		}            
	},
	/** 导出按钮操作 */
	handleExport(api, params, title) {
		this.$confirm('是否确认导出所有'+title+'数据项?', "警告", {
			confirmButtonText: "确定",
			cancelButtonText: "取消",
			type: "warning"
			}).then(() => {
				const loading = this.$loading({
          lock: true,
          text: '下载中，请稍等',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
			this.$request('get', api, params, (data, msg)=>{
				loading.close();
				this.download(data.data,title);        
			},msg => {
				loading.close();
			}, {responseType: 'blob'}, 'all')
		}).catch((action) => {
			
		});
	},
	download(data,title){
		let url = window.URL.createObjectURL(
			new Blob([data], { type: "application/vnd.ms-excel" })
		);
		let link = document.createElement("a");
		link.style.display = "none";
		link.href = url;
		link.setAttribute("download", title +".xlsx");
		document.body.appendChild(link);
		link.click();
	},
	async getDictList(type) {
		let list = [];
		let initData = this.$store.getters.initData || {};
		if (JSON.stringify(initData) != '{}') {
			if (initData.hasOwnProperty(type + '_list')) {
			list = initData[type + '_list'];
			} else {
			let res = await this.$request('get', '/admin/dict/type/' + type, {});
			if (res.length > 0) {
				list = this.$util.transformArray(res);
				initData[type + '_list'] = list;
				this.$store.commit('setInitData', initData);
			}
			}
		} else {
			let res = await this.$request('get', '/admin/dict/type/' + type, {});
			if (res.length > 0) {
				list = this.$util.transformArray(res);
				initData[type + '_list'] = list;
				this.$store.commit('setInitData', initData);
			}
		}
		return new Promise((resolve, reject) => {
			resolve(list);
		});
		},
	setColumns(data){
		let arr = this.columns.filter(item => item.prop == data);
		return arr[0].visible;
	},
	sortChange(column){
		let sort;
		if(column.order == "ascending") {
			sort = '1'
		} else if(column.order == "descending") {
			sort = '0'
		}
		this.getList();
	},
	handleOperate(type,row){
		this.$refs[type].init(row);
	},
}
}

export default mixin